﻿<template>
  <div class="app-container">
    <div class="chart-title">档案管理</div>
    <div class="chart-content">
      <div class="filter-title">
        <div></div>
        <div class="filter-right">
          <el-button type="primary"
                     @click="handleSelect">筛选</el-button>
          <el-button type="primary">导出</el-button>
        </div>
      </div>
      <div class="chart-list">
        <el-table :key="tableKey"
                  v-loading="listLoading"
                  :data="list"
                  element-loading-text="拼命加载中"
                  element-loading-spinner="el-icon-loading"
                  element-loading-background="rgba(255,255,255,0.2)"
                  border
                  fit
                  highlight-current-row
                  style="width: 100%min-height:1000px">
          <el-table-column fixed="left"
                           align="center"
                           :label="$t('table.id')"
                           width="65">
            <template slot-scope="scope">
              <span>{{
                (listQuery.page - 1) * listQuery.limit + scope.$index + 1
              }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="showTitle()">
            <template slot-scope="scope">
              <span>{{ scope.row.time }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('supplier.name')">
            <template slot-scope="scope">
              <span>{{ scope.row.supplier_name }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('goods.code_1')">
            <template slot-scope="scope">
              <span>{{ scope.row.goods_code }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('goods.name_1')">
            <template slot-scope="scope">
              <span>{{ scope.row.goods_name }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('public.number')">
            <template slot-scope="scope">
              <span>{{ scope.row.number }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('public.price')">
            <template slot-scope="scope">
              <span>{{ scope.row.price }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"
                           show-overflow-tooltip
                           :label="$t('public.money')">
            <template slot-scope="scope">
              <span>{{ scope.row.sum_money }}</span>
            </template>
          </el-table-column>

        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination background
                       :current-page="listQuery.page"
                       :page-sizes="[10, 20, 30, 50]"
                       :page-size="listQuery.limit"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total"
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"></el-pagination>
      </div>
      <!-- 编辑 -->
      <el-dialog :title="textMap[dialogStatus]"
                 :visible.sync="dialogFormVisible">
        <el-form ref="dataForm"
                 label-position="right"
                 label-width="30%"
                 style="width: 400px margin-left:120px"
                 size="small">
          <div class="pops">
            <div class="pops-normal">
              <el-form-item :label="$t('public.time') + ' :'"
                            prop="date">
                <el-radio-group v-model="listQuery.date">
                  <el-radio :label="1">月</el-radio>
                  <el-radio :label="3">季度</el-radio>
                  <el-radio :label="4">年</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item :label="$t('supplier.name') + ' :'">
                <drop-down-select :data="supplierOptions"
                                  :count="supplierCount"
                                  :page-size="pageSize"
                                  :id="listQuery.supplier_id"
                                  :name="listQuery.supplier_name"
                                  @getList="(pageNumber, search)=>{return getSupplier(pageNumber, search,'getSupplier')}"
                                  @addList="(pageNumber, search)=>{return getSupplier(pageNumber, search,'addSupplier')}"
                                  @choose="(data)=>{return choose(data, 'supplier')}"
                                  @remove="(data)=>{return remove(data, 'supplier')}"></drop-down-select>
              </el-form-item>
              <el-form-item :label="$t('goods.name_1') + ' :'">
                <drop-down-select :data="goodsOptions"
                                  :count="goodsCount"
                                  :page-size="pageSize"
                                  :id="listQuery.category_id"
                                  :name="listQuery.category_name"
                                  @getList="(pageNumber, search)=>{return getGoods(pageNumber, search,'getGoods')}"
                                  @addList="(pageNumber, search)=>{return getGoods(pageNumber, search,'addGoods')}"
                                  @choose="(data)=>{return choose(data, 'goods')}"
                                  @remove="(data)=>{return remove(data, 'goods')}"></drop-down-select>
              </el-form-item>
            </div>
          </div>
        </el-form>
        <div slot="footer"
             class="dialog-footer">
          <div>
          </div>
          <div>
            <el-button @click="dialogFormVisible = false">{{
              $t("table.cancel")
            }}</el-button>
            <el-button type="primary"
                       :disabled="filterDisabled"
                       @click="filterData">{{ $t("table.confirm") }}</el-button>
          </div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script type="text/javascript">
import dropDownSelect from "@/views/select/dropDownSelect"; // 分页选择控件
export default {
  name: "Itoc",
  components: {
    "drop-down-select": dropDownSelect
  },
  data() {
    return {
      showSearchItem: false,
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      checkAll: false,
      circleCheckAll: false,
      equipmentCheckAll: false,
      showDateStyle: 1,
      listQuery: {
        page: 1,
        limit: 20,
        date: 1,
        category_id: [],
        merchant_circle_id: [],
        itoc_id: undefined,
        admin_region_id: []
      },
      goodsSortOptions: [],
      goodsOptions: [],
      supplierOptions: [],
      supplierCount: undefined,
      goodsCount: undefined,
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        select: "筛选"
      },
      filterDisabled: false,
      pageSize: 20
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page,
        date: this.listQuery.date ? this.listQuery.date : 1,

        goods_name: this.listQuery.goods_name ? this.listQuery.goods_name : "",
        supplier_name: this.listQuery.supplier_name ? this.listQuery.supplier_name : ""
      };
      const path = {
        app: "goods",
        api: "api_goods_index_goodsProcurementStat",
        version: "1.0",
        type: 2,
        data: data
      };
      console.log('JSON.stringify(path)', JSON.stringify(path))
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    handleSelect() {
      this.dialogStatus = "select";
      this.dialogFormVisible = true;
    },
    // 筛选终端销售统计
    filterData() {
      if (!this.filterDisabled) {
        this.filterDisabled = true;
        this.getList();
        this.dialogFormVisible = false;
        this.showDateStyle = this.listQuery.date;
        setTimeout(() => {
          this.filterDisabled = false;
        }, 1000);
      }
    },
    // 多选
    handleCheckedCitiesChange(value, options, all) {
      let checkedCount = value.length;
      this[all] = checkedCount === this[options].length;
    },
    // 全选
    handleCheckAllChange(val, options, name) {
      let all = [];
      if (val) {
        const officers = this[options];
        all = officers.map(function(officer) {
          return officer.id
        });
      }
      this.listQuery[name] = all;
    },
    // 商品选择
    getGoods(pageNumber, search, name) {
      const path = {
        app: "goods",
        api: "api_goods_index_getList",
        version: "1.0",
        data: {
          rows: this.pageSize,
          page: pageNumber || 1,
          name: search || "",
          returnType: name || ""
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    getSupplier(pageNumber, search, name) {
      const path = {
        app: "archives",
        api: "api_archives_supplier_getList",
        version: "1.0",
        data: {
          rows: this.pageSize,
          page: pageNumber || 1,
          name: search || "",
          returnType: name || ""
        }
      };
      this.socketApi.sendSock(JSON.stringify(path), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },

    choose(data, name) {
      if (name === 'supplier') {
        this.listQuery.supplier_id = data.id;
        this.listQuery.supplier_name = data.name;
      } else if (name === 'goods') {
        this.listQuery.goods_id = data.id;
        this.listQuery.goods_name = data.name;
      }
    },
    remove(data, name) {
      if (name === 'supplier') {
        this.listQuery.supplier_id = undefined;
        this.listQuery.supplier_name = "";
      } else if (name === 'goods') {
        this.listQuery.goods_id = undefined;
        this.listQuery.goods_name = "";
      }
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "api_wxadmin_admin_keepLogin") {
            this.$store.dispatch("loginTransition", true);
            this.socketApi.sendCache(res => {
              this.socketData(res);
            })
          } else if (resj.api === "api_goods_index_goodsProcurementStat") {
            this.total = resj.count;
            this.list = resj.data;
            this.listLoading = false;
          } else if (resj.api === "api_goods_index_getList" && resj.type === "getGoods") {
            this.goodsOptions = resj.data;
            this.goodsCount = resj.count;
          } else if (resj.api === "api_goods_index_getList" && resj.type === "addGoods") {
            this.goodsOptions = this.goodsOptions.concat(resj.data);
          } else if (resj.api === "api_archives_supplier_getList" && resj.type === "getSupplier") {
            this.supplierOptions = resj.data;
            this.supplierCount = resj.count;
          } else if (resj.api === "api_archives_supplier_getList" && resj.type === "addSupplier") {
            this.supplierOptions = this.supplierOptions.concst(resj.data);
          }
        }
      }
    },
    // 显示时间格式
    showTitle() {
      switch (this.showDateStyle) {
        case 1:
          return '年（月份）';
        case 3:
          return '年（季度）';
        case 4:
          return '年';
        default:
          break;
      }
    }
  }
};
</script>
